'use strict';
import Base from './base.js';
export default class extends Base{

	// 现金流量
	async xianjinlogAction () {
		let times = JSON.parse(this.post('times'));
		let type = this.post('lx');
		let shopid = this.shop_userinfo.departmentid;

		let fanwei = {
			begintime: 0,
			endtime: 0
		}

		fanwei.begintime = Math.floor(new Date().setHours(0,0,0,0)/1000);
		fanwei.endtime = Math.floor(new Date().setHours(0,0,0,0)/1000) + 86400;

		if (times && (times[0] != null && times[1] != null)) {
			fanwei.begintime = Math.floor(new Date(times[0]).setHours(0,0,0,0)/1000)
			fanwei.endtime = Math.floor(new Date(times[1]).setHours(0,0,0,0)/1000) + 86400
 		}

		let res = [];
		if (type==1) {
			// 当前时间段账户金额收入
			res = await this.model('or_shop_accountlog')
					.alias('al')
					.field('al.accountid, al.orderid, sum(al.money) as summoney, a.name, a.accounttype')
					.join('think_or_shop_account a on a.id=al.accountid')
					.where('al.departmentid='+shopid+' and al.addtime >= '+fanwei.begintime+' and al.addtime < '+fanwei.endtime+' and al.type = 1')
					.group('al.accountid')
					.select();
		} else if (type==2) {
			// 当前时间段账户金额支出
			res = await this.model('or_shop_accountlog')
					.alias('al')
					.field('al.accountid, al.orderid, sum(al.money) as summoney, a.name, a.accounttype')
					.join('think_or_shop_account a on a.id=al.accountid')
					.where('al.departmentid='+shopid+' and al.addtime >= '+fanwei.begintime+' and al.addtime < '+fanwei.endtime+' and al.type = 2')
					.group('al.accountid')
					.select();
		} else if (type==3) {
			// 当前时间段账户总收益
			res = await this.model('or_shop_accountlog')
					.alias('al')
					.field('al.accountid, al.orderid, sum(al.money) as summoney, a.name, a.accounttype')
					.join('think_or_shop_account a on a.id=al.accountid')
					.where('al.departmentid='+shopid+' and al.addtime >= '+fanwei.begintime+' and al.addtime < '+fanwei.endtime)
					.group('al.accountid')
					.select();

		}

		return this.json(res)
	}
	
	// 每条现金流量的详细信息
	async lookinfoAction(){
		let data = this.post();

		let shopid = this.shop_userinfo.departmentid;

		let aid = data.id, sta = data.sta, end = data.end, pages = data.pages, limits = data.limits;

		let list = await this.model('or_shop_account')
				.alias('a')
				.field('a.id, al.type, al.ordertype, al.money, a.name, a.accounttype, al.addtime, al.orderid')
				.join('think_or_shop_accountlog al on a.id=al.accountid')
				.where('al.departmentid='+shopid+' and al.accountid='+aid+' and al.addtime >= '+sta+' and al.addtime < '+end)
				.page(pages, limits)
				.order('al.addtime DESC')
				.countSelect()

		return this.json(list);
	}

	// 各部门的现金业绩
	async getfeatinfoAction(){

		let times = JSON.parse(this.post('times'));

		let shopid = this.shop_userinfo.departmentid;

		let bmids = await this.model('or_department').where({pid: shopid, floor: 2}).getField('id');

		if (think.isEmpty(bmids)) return this.json([]);

		let begintime = Math.floor(new Date().setHours(0,0,0,0)/1000);
		let endtime = Math.floor(new Date().setHours(0,0,0,0)/1000) + 86400;

		if (times && (times[0] != null && times[1] != null)) {
			begintime = Math.floor(new Date(times[0]).setHours(0,0,0,0)/1000)
			endtime = Math.floor(new Date(times[1]).setHours(0,0,0,0)/1000) + 86400
 		}

		let bumenlist = await this.model('or_shop_yejiticheng')
				.alias('yj')
				.field('sum(yj.xianjinyeji) as total, d.name as dname, d.id as did')
				.join('`think_or_department` d on d.id=yj.bumenid')
				.where('yj.shopid = '+shopid+' AND yj.bumenid IN ('+bmids+') AND yj.addtime >= '+begintime+' AND yj.addtime < '+endtime)
				.group('yj.bumenid')
				.select();

		return this.json(bumenlist)
	}

	// 获取部门下小组的现金业绩
	async getfeatchildAction(){
		let pid = this.post('pid');

		let times = JSON.parse(this.post('times'));

		let shopid = this.shop_userinfo.departmentid;

		let xzids = await this.model('or_department').where({pid: pid, floor: 3}).getField('id');

		if (think.isEmpty(xzids)) return this.json([]);

		let begintime = Math.floor(new Date().setHours(0,0,0,0)/1000);
		let endtime = Math.floor(new Date().setHours(0,0,0,0)/1000) + 86400;

		if (times && (times[0] != null && times[1] != null)) {
			begintime = Math.floor(new Date(times[0]).setHours(0,0,0,0)/1000)
			endtime = Math.floor(new Date(times[1]).setHours(0,0,0,0)/1000) + 86400
 		}

		let xiaozulist = await this.model('or_shop_yejiticheng')
				.alias('yj')
				.field('sum(yj.xianjinyeji) as total, d.name as dname, d.id as did')
				.join('`think_or_department` d on d.id=yj.xiaozuid')
				.where('yj.shopid='+shopid+' AND yj.xiaozuid IN ('+xzids+') AND yj.addtime >= '+begintime+' AND yj.addtime < '+endtime)
				.group('yj.xiaozuid')
				.select();

		return this.json(xiaozulist)
	}

	// 各部门或小组详细业绩列表
	async getfeatlistAction(){
		let id = this.post('pid');
		let floor = this.post('floor');
		let times = JSON.parse(this.post('times'));
		let page = JSON.parse(this.post('page'));

		let shopid = this.shop_userinfo.departmentid;

		let begintime = Math.floor(new Date().setHours(0,0,0,0)/1000);
		let endtime = Math.floor(new Date().setHours(0,0,0,0)/1000) + 86400;

		if (times && (times[0] != null && times[1] != null)) {
			begintime = Math.floor(new Date(times[0]).setHours(0,0,0,0)/1000)
			endtime = Math.floor(new Date(times[1]).setHours(0,0,0,0)/1000) + 86400
 		}

		let str = '';

		if (floor == 2) str = 'yj.shopid = ' + shopid + ' AND yj.bumenid = ' + id + ' AND yj.addtime >= ' + begintime + ' AND yj.addtime < ' + endtime;
		else str = 'yj.shopid = ' + shopid + ' AND yj.xiaozuid = ' + id + ' AND yj.addtime >= ' + begintime + ' AND yj.addtime < ' + endtime;

		let yjlist = await this.model('or_shop_yejiticheng')
				.alias('yj')
				.field('yj.*, u.name, u.worknum')
				.join('`think_or_app_user` u on u.id=yj.userid')
				.where(str)
				.page(page.pages, page.limits)
				.order('yj.addtime DESC')
				.countSelect();

		return this.json(yjlist)
	}

}